Ext.namespace("Milan.widget");

Milan.widget.windowArray = new Array();
Milan.widget.MAX_ZINDEX = 99;
Milan.widget.DEFAULT_CONFIRM_BTN_CAPTION = "确 定";
Milan.widget.DEFAULT_CANCEL_BTN_CAPTION = "取 消";
Milan.widget.ANIMATE_TYPE_MOVERESIZE = 1;
Milan.widget.ANIMATE_TYPE_OPACITY	 = 2;
Milan.widget.ANIMATE_DURATION	     = 0.5;			//默认动画持续时间，单位:秒

var dialogContainerId = "dialogDiv";
Milan.widget.Window = Class.create();
Milan.widget.Window.prototype = 
{
		/*---------------- initializer ------------------*/
	initialize : function(_title,_msg,_options)
	{
		this.options = {
			width           : "300px",
			height          : "100px",
			top							: undefined,
			left						: undefined,
			dialogClass     : "popup popup_width",
			titleClass      : "popup_top_left",
			bodyClass       : "win-body",
			bodyContentClass: "win-body-content",
			bottomClass     : "win-btn-panel",
			bottomLeftClass	: "popup_btm_left",
			bottomRightClass: "popup_btm_right",
			bottomLineClass : "win-foot-l",
			needCloseParent : false,
			animateShow	: true,			//动画显示
			animateClose	: true,		//动画关闭
			animateType	: Milan.widget.ANIMATE_TYPE_MOVERESIZE, //动画类型，默认是追踪框
			animateDuration : Milan.widget.ANIMATE_DURATION,		//动画持续时间，单位:秒
			eventX		: Milan.commonutils.mouseX,			
			eventY		: Milan.commonutils.mouseY,
			closeCallBack : null,
			drag		: true	,			//允许拉动
			noHead	: false,			//没有头部，这个为false，自然不能拉到
			confirmBtnTxt : Milan.widget.DEFAULT_CONFIRM_BTN_CAPTION,
			cancelBtnTxt : Milan.widget.DEFAULT_CANCEL_BTN_CAPTION,
			enableCloseBtn:true			//是否允许出现关闭按钮
		};
		
		this.id = "__widget_window__";
		this.title = _title;
		this._init(_title,_msg,_options);
		this._createDialog();
		if (this.options.drag && this.titleDiv) Milan.drag.init( this.titleDiv, this.dialog);
	},
	
	/*---------------- private method ------------------*/
	_init : function(_title,_msg,_options)
	{
		this.closeWindowHrefId = "_close_hrefId_" + parseInt(Math.random()*10000);
		this.title = _title || "提示信息";
		this.msg = _msg || "";
		this.setOptions(_options);
	},
	setOptions : function(_options)
	{
		this.options = Object.extend(this.options,_options ||{})
	},
	
	getOptions : function()
	{
		return this.options;
	},
	
	_createDialog : function()
	{
		if (!this.options.noBG)
		{
			var bgDiv = Milan.widget._generateBg();
			this.bgDiv = bgDiv;
		}
		var dialog = Milan.widget._createEle("DIV");		//大窗体
		dialog.id = Milan.widget.Window.id;
		dialog.className = this.options.dialogClass;
		this.dialog = dialog;
		
		if (!this.options.noHead)			//无头
		{
			var titleDiv = this._createTitle();			//title区
			this.titleDiv = titleDiv;
		}
		var bodyDiv = this._createBody();				//body区
		var bottomLineDiv =  this._createBottomLine();		//bottom区
		this.bottomLineDiv = bottomLineDiv;
		
		var dialogContainer = this._getDialogContainer();
		dialogContainer.appendChild(dialog);
		if (this.bgDiv) dialogContainer.appendChild(this.bgDiv);
		if (titleDiv) dialog.appendChild(titleDiv);
		dialog.appendChild(bodyDiv);
		dialog.appendChild(bottomLineDiv);
		
		if (this.options.width)
		{
			dialog.style.width = parseInt(this.options.width)+"px";
			/*if (Milan.navigator.isIE && Milan.navigator.version.indexOf("6")==0)
				bodyDiv.style.width = (parseInt(this.options.width)-16)+"px"*/
		}

		this.setPositionUseOptions();
		//$(this.closeWindowHrefId).onclick = function(){this.closeDialog(this.options.closeCallBack)}.bind(this);
		
		
		//如果bgDiv下有iframe的话...
		if (this.bgDiv && this.bgDiv.childNodes.length>0)
			this.bgDiv.childNodes[0].style.zIndex = Milan.widget.MAX_ZINDEX + 1;
		if (this.bgDiv) this.bgDiv.style.zIndex = Milan.widget.MAX_ZINDEX + 2;
		this.dialog.style.zIndex = Milan.widget.MAX_ZINDEX + 4;
		Milan.widget.MAX_ZINDEX +=4;
		this.dialog.focus();
		Milan.widget.windowArray.push(this);
	},
	
	_getDialogContainer:function()
	{
		return document.getElementById(dialogContainerId);
	},
	
	_createTitle : function(){
		var titleLDiv = Milan.widget._createEle("DIV");		//头部DIV-LEFT
		titleLDiv.className = this.options.titleClass;
		
		var titleRDiv = Milan.widget._createEle("DIV");	//头部DIV-RIGHT
		titleRDiv.className = "popup_top_right";
		
		


		if (this.options.enableCloseBtn)
		{
			var closeDiv = Milan.widget._createEle("DIV");		//头部中的close的div
			closeDiv.className = "pop_close";			
			var closeBtn = Milan.widget._createEle("a");		//头部中的关闭按钮
			closeBtn.className = "pop_hint_close";
			closeBtn.href="javascript:void(0)";
			closeBtn.title="关闭";
			closeBtn.onclick=function(){this.closeDialog(this.options.closeCallBack)}.bind(this);
		}
		var titleMsgDiv = Milan.widget._createEle("H2");		//头部的TITLE信息
		titleMsgDiv.innerHTML=this.title.escapeHtmlChar();
		
		if (closeDiv)
		{
			closeDiv.appendChild(closeBtn);
			titleRDiv.appendChild(closeDiv);
		}
		titleRDiv.appendChild(titleMsgDiv);
		titleLDiv.appendChild(titleRDiv);
		return titleLDiv;
	},
	
	_createBottomLine : function(){
		var bottomLDiv =  Milan.widget._createEle("DIV");
		bottomLDiv.className = this.options.bottomLeftClass;
		
		var bottomRDiv =  Milan.widget._createEle("DIV");
		bottomRDiv.className = this.options.bottomRightClass;

		bottomLDiv.appendChild(bottomRDiv);
		return bottomLDiv;
	},
	
	_createBody : function()
	{
		
		var bodyLDiv = Milan.widget._createEle("DIV");
		bodyLDiv.className = "popup_main_left";
		
		var bodyRDiv = Milan.widget._createEle("DIV");
		bodyRDiv.className = "popup_main_right";
		
		var bodyScrollDiv = Milan.widget._createEle("DIV"); 
		bodyScrollDiv.id = "_milan_widget_window_bodyScroll_id_";


		var bodyContentDiv = Milan.widget._createEle("DIV");
		bodyContentDiv.className = "popup_main_content";
		bodyContentDiv.innerHTML = this.msg;
		
		this.body = bodyContentDiv;
		this.bodyScroll = bodyScrollDiv;
		
		bodyScrollDiv.appendChild(bodyContentDiv);
		bodyRDiv.appendChild(bodyScrollDiv);
		bodyLDiv.appendChild(bodyRDiv);
		return bodyLDiv;
		
	},
	
	_createBtnZone: function()
	{
		var btnZoneDiv = Milan.widget._createEle("DIV");
		btnZoneDiv.className = this.options.bottomClass;
		this.bodyScroll.parentNode.appendChild(btnZoneDiv);
		this._btnZoneDiv = btnZoneDiv;
		return btnZoneDiv;
	},
	
	appendBtn : function(btn)
	{
		if (!this._btnZoneDiv)
			this._createBtnZone();
			
		this._btnZoneDiv.appendChild(btn);	
		this._btnZoneDiv.appendChild(document.createTextNode("    "));
	},
	
	closeDialog : function(callBack)
	{
		if(callBack)
		{
	    if(callBack()==false) return false;
		}
		if (this.options.animateClose) 
		{
			if (this._animateClose(this._closeDialog.bind(this)))
				return true;
			else
				return this._closeDialog();
		}
		else
			return this._closeDialog();
		
		
	},
	
	_closeDialog:function()
	{
		var dialogContainer = this._getDialogContainer();	
		while (this.bgDiv != null) 
		{
			try{
				dialogContainer.removeChild(this.bgDiv);
			}
			catch(e){
			}
			this.bgDiv = null;
		}
		while (this.dialog != null) 
		{
			try{
				dialogContainer.removeChild(this.dialog);
			}
			catch(e){
			}
			this.dialog = null;
		}
		Milan.widget.MAX_ZINDEX -= 4;
		//window.onresize = null;
		Milan.widget.windowArray.pop(this);
		return true;
	},
	
	_animateClose:function(closeFun)
	{
		
		switch(this.options.animateType)
		{
			case Milan.widget.ANIMATE_TYPE_MOVERESIZE:				
				var dialogContainer = this._getDialogContainer();
				if (!this.dialog) 
				{
					closeFun();
					return true;
				}
				var animDiv = Milan.widget._createEle("DIV");
				animDiv.style.left = parseInt(this.dialog.style.left)+"px";
				animDiv.style.top = parseInt(this.dialog.style.top)+"px";
				animDiv.style.width = parseInt(this.dialog.clientWidth)+"px";
				animDiv.style.height = parseInt(this.dialog.clientHeight)+"px";
				animDiv.style.zIndex = this.dialog.style.zIndex;
				animDiv.className = "animWindow";	
				dialogContainer.appendChild(animDiv);
				window.setTimeout(
					Milan.commonutils.animateMoveResize,20,animDiv,this.options.eventY,this.options.eventX,0,0,this.options.animateDuration,function(){dialogContainer.removeChild(animDiv);}
					);
				closeFun();
				return true;
			break;
			case Milan.widget.ANIMATE_TYPE_OPACITY:
				Milan.commonutils.animateOpacity(this.dialog,0,this.options.animateDuration,closeFun);		
				return true;
			break;
		}
		return false;
		
		

	},
	
	_animateShow:function()
	{
		this._directShow();
		this.hide();		//为了在IE下能获得dialog的高宽
		
		switch(this.options.animateType)
		{
			case Milan.widget.ANIMATE_TYPE_MOVERESIZE:				
				var dialogContainer = this._getDialogContainer();
				var animDiv = Milan.widget._createEle("DIV");
				animDiv.style.left = parseInt(this.options.eventX)+"px";
				animDiv.style.top = parseInt(this.options.eventY)+"px";
				animDiv.style.width = "0px";
				animDiv.style.height = "0px";
				animDiv.style.zIndex = this.dialog.style.zIndex;
				animDiv.className = "animWindow";	
				dialogContainer.appendChild(animDiv);
				window.setTimeout(
					Milan.commonutils.animateMoveResize,20,animDiv,this.dialog.style.top,this.dialog.style.left,this.dialog.clientWidth,this.dialog.clientHeight,this.options.animateDuration,function(){
							dialogContainer.removeChild(animDiv);
							this._directShow();
							//为form表单的第一个控件获取焦点
							var form = $(this.body).select("form");
							if(form && form.length > 0)
							{
								var ele=form[0].findFirstElement();
								if(ele && !ele.readOnly) ele.activate();
							}
						}.bind(this)
					);
			break;
			case Milan.widget.ANIMATE_TYPE_OPACITY:
				Milan.commonutils.setOpacity(this.dialog,0);
				this._directShow();
				Milan.commonutils.animateOpacity(this.dialog,100,this.options.animateDuration,this._directShow.bind(this));		
			break;
		}
		
		

	},
	
	_directShow:function()
	{
		this.dialog.style.visibility = "visible";
		this.setPositionUseOptions();
		if (this.options.onShow) 
			this.options.onShow();
	},
	
	
	
	setContent : function(contentstr)
	{
		this.body.innerHTML = contentstr;
		this.setPositionUseOptions();
		return this.body;
	},
		
	update : function(contentstr)
	{
		return this.setContent(contentstr);
	},
	appendChild : function(domObj)
	{
		this.body.insertBefore(domObj,this.body.firstChild);
	},
	getBodyDom : function()
	{
		return this.body;
	},
	setSize : function(size)
	{
		//$(Milan.widget.Window.id).style.width  = size["width"];
		//$(Milan.widget.Window.id).style.height = size["height"];
		if (size)
		{
			if (size["width"])
				this.bodyScroll.style.width=size["width"];
			if (size["height"])
				this.bodyScroll.style.height = size["height"];
		}
		return this;
	},
	setOverflow:function(overflow){
		this.bodyScroll.style.overflow = overflow;
		return this;
	},
	setPositionUseOptions : function()
	{
		this.setPosition(this.options.top,this.options.left);
	},
	
	setPosition : function(top,left)
	{
		if (this.dialog.scrollHeight>=screen.availHeight)
		{
			this.bodyScroll.style.overflow = "auto";
			this.bodyScroll.style.height = (document.body.clientHeight -70)+"px";
		}
		
		if (top)
			this.dialog.style.top = parseInt(top)+"px";
		else
			this.dialog.style.top = (document.body.scrollTop + ((document.body.clientHeight - this.dialog.offsetHeight)/2))+"px";
		
		if (left)
			this.dialog.style.left = parseInt(left)+"px";
		else
			this.dialog.style.left = (screen.availWidth - parseInt(this.dialog.style.width))/2 + "px";

		if (parseInt(this.dialog.style.top)<=0) this.dialog.style.top="0px";
		if (parseInt(this.dialog.style.left)<=0) this.dialog.style.left="0px";
		if (Milan.navigator.isIE && Milan.navigator.version.indexOf("6")==0)		//IE6的话需要重新计算title和bottom的width
		{
			this.titleDiv.style.width = this.dialog.clientWidth-3; 
			this.bottomLineDiv.style.width = this.dialog.clientWidth-3; 
		}
	},
	releaseMem : function(oEle)  
  {
    if(!oEle)oEle=document.body;  
    var atr=oEle.attributes;  
    if(atr)  
    for(var i=0,j=atr.length;i<j;i++)
        if(typeof oEle[atr[i].name]== 'function ') oEle[atr[i].name]=null;  
    if(oEle.childNodes)
      for(var i=0,j=oEle.childNodes.length;i<j;i++)  
        arguments.callee(oEle.childNodes[i]);
  },
  hide: function()
	{
		this.dialog.style.visibility = "hidden";
  },
  show: function() 
	{		
		if (this.options.animateShow)
			this._animateShow();
		else
			this._directShow();
  }
}

Milan.widget.Dialog = function(_title,_msg,_options)
{
	this.initialize(_title,_msg,_options);	

	var btnYes = Milan.widget._createBtn(this.options.confirmBtnTxt,function(){this.closeDialog(this.options.confirmCallback)}.bindAsEventListener(this));
	this.appendBtn(btnYes);	
	
	this.show();
}
Object.extend(Milan.widget.Dialog.prototype,Milan.widget.Window.prototype);

Milan.widget.ConfirmDialog = function(_title,_msg,_options)
{
	this.initialize(_title,_msg,_options);	
	if(this.options.thirdBtnTxt && this.options.thirdBtnCallback)
	{
		var thirdBtn = Milan.widget._createBtn(this.options.thirdBtnTxt,function(){this.closeDialog(this.options.thirdBtnCallback)}.bindAsEventListener(this),this.options.thirdBtnClass);
		this.appendBtn(thirdBtn);		
	}
		
	var btnYes = Milan.widget._createBtn(this.options.confirmBtnTxt,function(){this.closeDialog(this.options.confirmCallback)}.bindAsEventListener(this));
	this.appendBtn(btnYes);	
	
	var btnNo = Milan.widget._createBtn(this.options.cancelBtnTxt,function(){this.closeDialog(this.options.cancelCallback)}.bindAsEventListener(this));
	this.appendBtn(btnNo);
	
	this.show();
}
Object.extend(Milan.widget.ConfirmDialog.prototype,Milan.widget.Window.prototype);

Milan.widget._generateBg = function()
{
	var bgDiv = Milan.widget._createEle("DIV");
	
	bgDiv.className = "masker";
	var bodyHeight = document.body.scrollHeight;
	var botHeight = $("bottomFrame").positionedOffset().top+$("bottomFrame").getHeight();
	bgDiv.style.height = ((bodyHeight>botHeight)?bodyHeight:botHeight)+"px";
	if (Milan.navigator.isIE || Milan.navigator.isAndroid)	//如果是IE的话还需要加入一个iframe，为了盖住combobox之类的控件
	{
		var iframe = Milan.widget._createEle("iframe");
		iframe.className = "iframe-masker";
		bgDiv.appendChild(iframe);
		iframe.style.height = ((bodyHeight>botHeight)?bodyHeight:botHeight)+"px";
		bgDiv.iframe = iframe;
	}
	return bgDiv;
}

Milan.widget._createBtn = function(caption,callback,className)
{
	className = className ||'btn_short';
	var btn = Milan.widget._createEle("a");
	//btnYes.type = "button";		//5.0版本改用a标签替代btn
	btn.className = className;
	btn.innerText = caption;
	if (callback)
		btn.onclick = callback;
	return btn;
}

Milan.widget._createEle = function(tag)
{
	return document.createElement(tag);
}


//关闭最后一个窗体
Milan.widget.closeCurrentDialog = function()
{
	var closingWindow = Milan.widget.windowArray[Milan.widget.windowArray.length-1];
	if(closingWindow) 
		return closingWindow.closeDialog(closingWindow.options.closeCallBack);
}


//关闭所有窗体
Milan.widget.closeAllDialog = function()
{

	for(var i=Milan.widget.windowArray.length;i>=0;i--)
	{
		var closingWindow = Milan.widget.windowArray[i-1];
		if(closingWindow) 
			if (!closingWindow.closeDialog(closingWindow.options.closeCallBack))
				return false;		
	}
	return true;
}
